package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.VolumeGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Question;
import com.itheima.pojo.Questionnaire;
import com.itheima.service.VolumeGroupservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Stream;

@Service(interfaceClass = VolumeGroupservice.class)
@Transactional
public class VolumeGroupserviceImpl implements VolumeGroupservice {
    @Autowired
    private VolumeGroupDao volumeGroupDao;


    @Override
    public int addVolumeGroup(Integer[] ids,Questionnaire questionnaire) {
        volumeGroupDao.addVolumeGroup(questionnaire);
        Stream.of(ids).forEach(s -> volumeGroupDao.setVolume(s,questionnaire.getId()));
        return 1;
    }

    @Override
    public int addVolume(Question question) {
        return volumeGroupDao.addVolume(question);
    }

    @Override
    public List<Question> selectQuestion() {
        return volumeGroupDao.selectQuestion();
    }

    @Override
    public void UpdateVolume(Integer[] ids,Questionnaire questionnaire) {
        //更新主表
        volumeGroupDao.UpdateVolumeGroup(questionnaire);

        volumeGroupDao.delIreById(questionnaire.getId());
        for (Integer id : ids) {
            volumeGroupDao.insertIreById(id,questionnaire.getId());
        }
    }

    @Override
    public List<Integer> selectids(Integer id) {
        return volumeGroupDao.selectids(id);
    }

    @Override
    public int UpdateVolumeGroup(Questionnaire questionnaire) {
        return volumeGroupDao.UpdateVolumeGroup(questionnaire);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());

        //模糊查询
        String queryString = queryPageBean.getQueryString();
        if (queryString != null && queryString.length() > 0){
            queryString = "%" + queryString + "%";
            queryPageBean.setQueryString(queryString);
        }

        //返回数据
        Page<Questionnaire> list = volumeGroupDao.findByCondition(queryPageBean);
//        System.out.println(list);
//        PageInfo<Questionnaire> info = new PageInfo<>(list);
        long total = list.getTotal();
//        System.out.println(total);
        return new PageResult(total,list);
    }


    @Override
    public void delById(Integer id) {
        volumeGroupDao.delById(id);
    }
}
